package com.yanqu.road.server.cmd.union;

import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

//@Cmd(code = Protocol.S_UNION_SET_NOTICE_READ_STATE, desc = "设置公告阅读状态")
public class SetNoticeReadStateCmd extends AbstractCommand {

	private int state;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionSetNoticeReadStateReqMsg reqMsg = UnionProto.UnionSetNoticeReadStateReqMsg.parseFrom(packet.getBytes());
		state = reqMsg.getState();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		String unionUid = player.getUserInfo().getUnionUid();
		if(StringUtils.isNullOrEmpty(unionUid)){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		UnionMember member = UnionMgr.getUnionMember(unionUid, player.getUserId());
		if(null == member){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		member.setNoticeReadState(state);
		player.sendPacket(Protocol.U_UNION_DETAIL, UnionMgr.getUnionDetailMsg(unionUid, eUnionSyncType.Normal.getValue()));
	}

	@Override
	public void sendData(GamePlayer player) {
		UnionProto.UnionSetNoticeReadStateRespMsg.Builder respMsg = UnionProto.UnionSetNoticeReadStateRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_UNION_SET_NOTICE_READ_STATE, respMsg);
	}

}
